package net.template.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.template.domain.template.*;
import net.template.repository.AttrDataRepository;
import net.template.repository.DataIdRepository;
import net.template.repository.TemplateAttrRepository;
import net.template.repository.TemplateRepository;
import net.template.service.controller.BaseController;
import net.template.service.controller.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.*;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/9/16/016.
 */
@RestController
@CrossOrigin
@RequestMapping("attrPreView")
public class AttrViewService extends BaseController {
    @Autowired
    private TemplateRepository templateRepository;

    @Autowired
    private TemplateAttrRepository templateAttrRepository;

    @Autowired
    private AttrDataRepository attrDataRepository;

    @Autowired
    private DataIdRepository dataIdRepository;

    /**
     * 预览模板属性列表
     * @param id
     * @return
     */
    @Transactional
    @GetMapping("/{id}")
    public Result<JSONObject> previewTemplateAttr(@PathVariable("id") Integer id){
        List<TemplateAttr> templateAttrs = templateAttrRepository.findByTemplateId(id);
        List formItemList = new ArrayList<>();
        //List returnItemList = new ArrayList<>();
        for (TemplateAttr templateAttr : templateAttrs) {
            if (!(templateAttr.getAttr().equals("id"))) {
                FormItem formItem = new FormItem();
                formItem.setType("input");
                formItem.setLabel(templateAttr.getAttrCn());
                formItem.setKey(templateAttr.getAttr());
                formItem.setSubtype("input");
                formItemList.add(formItem);
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("templateId", id);
        jsonObject.put("formItemList", JSONArray.toJSON(formItemList));

        String jsonString = JSONObject.toJSONString(jsonObject);
        return Result.success(jsonObject);
    }

    /**
     * 新增数据
     * @param input
     * @return
     */
    @Transactional
    @PostMapping("/add")
    public Result<Void> createData(@RequestBody List<AttrSimple> input){
        List<AttrData> attrDataArrayList = new ArrayList<AttrData>();


        //生成每个提交的唯一id
        Integer maxId = dataIdRepository.findMax();
        input.forEach(c->{
            //关键
            AttrData attrData = new AttrData();
            attrData.setTemplateId(c.getTemplateId());
            attrData.setDataId(maxId);
            attrData.setAttrId(c.getName());
            attrData.setAttrValue(c.getValue());
            attrDataArrayList.add(attrData);
        });
        attrDataRepository.save(attrDataArrayList);
        return Result.success();
    }
}
